/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.pebom.util;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import org.pebom.empresa.Cliente;

/**
 *
 * @author Drury
 */
public class ClientesDAO {
     private static ClientesDAO clientesDAO;

    public static ClientesDAO getInstance() {
        if (clientesDAO == null) {
            clientesDAO = new ClientesDAO();
        }
        return clientesDAO;
    }



   public void insertCliente(Cliente cliente ) throws SQLException {
       
       System.out.println("teste");
        String sql = "insert into clientes (cpf,nome) values (" + cliente.getCpf() + ",'"  + cliente.getNome() + "')";
        BD.getInstance().getConn().createStatement().execute(sql);
        System.out.println("Inseriu Cliente no BD");
    }
   
//   public int buscaID() throws SQLException {
//       String sql = "select MAX(id) from recursos";
//       ResultSet resultInt = BD.getInstance().getConn().createStatement().executeQuery(sql);
//       int id = 1;
//       while (resultInt.next()){
//            id = resultInt.getInt(1);
//       }
//      
//       return id;
//   }

    public List<Cliente> buscar() throws SQLException {
        List<Cliente> clientes = new ArrayList<Cliente>();
        String sql = "select cpf,nome from clientes";
        ResultSet results = BD.getInstance().getConn().createStatement().executeQuery(sql);


        while (results.next()) {
            Cliente cliente = new Cliente();
            cliente.setCpf(results.getInt(1));
            cliente.setNome(results.getString(2));
            clientes.add(cliente);

        }
        results.close();
        System.out.println("Buscou CLientes do Banco");
        return clientes;
    }
    
    
    public List<Cliente> buscar(int cpf,String nome) throws SQLException {
        List<Cliente> clientes = new ArrayList<Cliente>();
        String sql = "select cpf,nome from clientes where cpf ="+cpf+" OR nome='"+nome+"'";
        ResultSet results = BD.getInstance().getConn().createStatement().executeQuery(sql);


        while (results.next()) {
            Cliente cliente = new Cliente();
            cliente.setCpf(results.getInt(2));
            cliente.setNome(results.getString(3));
            clientes.add(cliente);

        }
        results.close();
        return clientes;
    }
    
    
    public void remover(int cpf,String nome) throws SQLException {
        String sql = "delete from clientes where cpf ="+cpf+" OR nome='"+nome+"'";
        BD.getInstance().getConn().createStatement().execute(sql);

        System.out.println("Cliente Deletado");
    }
    
    
    
    
    
}
